package com.learn.config;

import com.learn.domain.Pet;
import com.learn.interceptor.LoginInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author mpk
 * @since 2024/8/16 18:05:34
 */

@Configuration
@Slf4j
public class WebMvcConfiguration implements WebMvcConfigurer {


    @Override
    public void addFormatters(FormatterRegistry registry) {
        WebMvcConfigurer.super.addFormatters(registry);
        registry.addConverter(
                new Converter<String, Pet>() {
                    @Override
                    public Pet convert(String source) {
                        log.info("source:{}",source);
                        if(source.contains("&"))
                        {
                            String[] strings = source.split("&");
                            return new Pet(strings[0],Integer.parseInt(strings[1]));
                        }
                        return null;

                    }
                }
        );

    }

//    @Override
//    public void addInterceptors(InterceptorRegistry registry) {
//        WebMvcConfigurer.super.addInterceptors(registry);
//        registry.addInterceptor(new LoginInterceptor())
//                .addPathPatterns("/**")
//                .excludePathPatterns("/login.html","/login","/css/**","/js/**","/images/**","/fonts/**" );
//    }
}
